﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ProTrack.Api.Helper;
using ProTrack.Dto;
using ProTrack.Entity;
using ProTrack.IServices.StoreHouse;
using ProTrack.Report;
using ProTrack.Utility;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace ProTrack.Api.Controllers
{
    /// <summary>
    /// 仓库管理 库拉接口
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class WmWarehouseLocationController : ControllerBase
    {
        public WmWarehouseLocationController(IWmWarehouseLocationServices services, LogHelper LogHelper)
        {
            Services = services;
            this.LogHelper = LogHelper;
        }
        public IWmWarehouseLocationServices Services { get; }
        public LogHelper LogHelper { get; }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] WmWarehouseLocation model)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                model.CreateTime = StringHelper.GetTimeNow();
                model.Creator = User.Identity.Name;

                var isSuccess = await Services.AddAsync(model);
                if (isSuccess)
                {
                    status.Msg = "新增成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "新增失败，请检查数据";
                }

            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常,请稍后从试";
                await LogHelper.WriteErrorLog("仓库库位-新增数据", ex, HttpContext);
            }
            return Ok(status);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Update([FromForm] WmWarehouseLocation model)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                model.UpdateTime = StringHelper.GetTimeNow();
                model.Updater = User.Identity.Name;
                var isSuccess = await Services.UpdateAsync(model);
                if (isSuccess)
                {
                    status.Msg = "修改成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "修改失败，请检查数据";
                }
            }
            catch (Exception ex)
            {
                status.Code= -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("仓库库位-修改数据", ex, HttpContext);
            }
            return Ok(status);
        }
        /// <summary>
        /// 获取所有数据
        /// </summary>
        /// <returns></returns>
        [HttpGet("GeyAll")]
        public async Task<IActionResult> GeyAll()
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var list = await Services.GetAllAsync();

                status.Data = list;
                status.Count = list.Count();
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常,请稍后从试";
                //写日志
                await LogHelper.WriteErrorLog("仓库库位-获取数据", ex, HttpContext);
            }
            return Ok(status);
        }
        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="LocationNumber"></param>
        /// <param name="LocationName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get([FromQuery] int PageIndex, [FromQuery] int PageSize, [FromQuery] string LocationNumber, [FromQuery] string LocationName)
        {

            var status = BaseStatus.CreateInstance();
            try
            {
                LocationNumber = LocationNumber ?? "";
                LocationName = LocationName ?? "";

                var list = await Services.GetAsync(PageIndex, PageSize, LocationNumber, LocationName);
                status.Data = list;
                status.Count = await Services.GetCountAsync(LocationNumber, LocationName);
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("计量单位-分页获取", ex, HttpContext);
            }
            return Ok(status);
        }
        /// <summary>
        /// 删除班组
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Deletees([FromForm] int Id)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var isSuccess = await Services.DeleteAsync(Id);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败,请稍后重试!";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常，请稍后重试！";
                //写日志
                await LogHelper.WriteErrorLog("班组类型-删除", ex, HttpContext);
            }
            return Ok(status);
        }
        /// <summary>
        /// 批量删除班组类型
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpDelete("Deleteees")]
        public async Task<IActionResult> Deleteees([FromForm] string Ids)
        {
            var status = BaseStatus.CreateInstance();
            try
            {
                var arr = Ids.SplitId();
                var isSuccess = await Services.DeleteAsync(arr);
                if (isSuccess)
                {
                    status.Msg = "删除成功";
                }
                else
                {
                    status.Code = -1;
                    status.Msg = "删除失败,请稍后重!";
                }
            }
            catch (Exception ex)
            {
                status.Code = -1;
                status.Msg = "服务器异常,请稍后重试!";
                await LogHelper.WriteErrorLog("班组类型-批量删除", ex, HttpContext);
            }
            return Ok(status);
        }

        [HttpGet("Export")]
        public async Task<IActionResult> Export()
        {
            try
            {

                var list = await Services.GetAllAsync();
                var TitleArray = new ReportDomain[] {
                    new ReportDomain{ Title="序号",Fileld="No"},
                    new ReportDomain{ Title="库位编码",Fileld="LocationNumber"},
                    new ReportDomain{ Title="库位名称",Fileld="LocationName"},
                    new ReportDomain{ Title="所属仓库",Fileld="WarehouseName"},
                    new ReportDomain{ Title="所属库区",Fileld="AreaName"},
                    new ReportDomain{ Title="位置",Fileld="Location"},
                    new ReportDomain{ Title="面积",Fileld="Area"},
                    new ReportDomain{ Title="负责人",Fileld="Charge"},
                    new ReportDomain{ Title="备注",Fileld="Remark"},
                };
                var fileArray = BaseReport.Export(list, "库位信息", TitleArray);

                return File(new System.IO.MemoryStream(fileArray), "application/octet-stream", "库位信息.xls");
            }
            catch (Exception ex)
            {

                //此处应当日志记录
                await LogHelper.WriteErrorLog("库位-导出", ex, HttpContext);
                return new JsonResult(BaseStatus.CreateInstance(-1, "导出失败，服务器异常请稍后再试！"));
            }
        }
    }
}
